/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.BusinessConfig;
import com.boxin.ims.modules.wechat.entity.share.RuleType;
import com.boxin.ims.modules.wechat.service.BusinessConfigService;

/**
 * 微信特殊服务配置类Controller
 * @author Jakemanse
 * @version 2013-09-27
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/businessConfig")
public class BusinessConfigController extends BaseController {

	@Autowired
	private BusinessConfigService businessConfigService;
	
	@ModelAttribute
	public BusinessConfig get(@RequestParam(required=false) Long id) {
		if (id != null){
			return businessConfigService.get(id);
		}else{
			return new BusinessConfig();
		}
	}
	
	@RequiresPermissions("wechat:businessConfig:view")
	@RequestMapping(value = {"list", ""})
	public String list(BusinessConfig businessConfig, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<BusinessConfig> page = businessConfigService.find(new Page<BusinessConfig>(request, response), businessConfig); 
        
        List<RuleType> ruleMapList = RuleType.ruleMapList;
		model.addAttribute("businessConfig", businessConfig);
		model.addAttribute("ruleMapList", ruleMapList);
		
        model.addAttribute("page", page);
		return "modules/wechat/businessConfigList";
	}

	@RequiresPermissions("wechat:businessConfig:view")
	@RequestMapping(value = "form")
	public String form(BusinessConfig businessConfig, Model model) {
		
		List<RuleType> ruleMapList = RuleType.ruleMapList;
		model.addAttribute("businessConfig", businessConfig);
		model.addAttribute("ruleMapList", ruleMapList);
		
		
		return "modules/wechat/businessConfigForm";
	}

	@RequiresPermissions("wechat:businessConfig:edit")
	@RequestMapping(value = "save")
	public String save(BusinessConfig businessConfig, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, businessConfig)){
			return form(businessConfig, model);
		}
		businessConfigService.save(businessConfig);
		addMessage(redirectAttributes, "保存微信特殊服务配置类'" + businessConfig.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/businessConfig/?repage";
	}
	
	@RequiresPermissions("wechat:businessConfig:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		businessConfigService.delete(id);
		addMessage(redirectAttributes, "删除微信特殊服务配置类成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/businessConfig/?repage";
	}

}
